/*
 * @lc app=leetcode.cn id=46 lang=javascript
 *
 * [46] 全排列
 */

// @lc code=start
var permutePath = function (nums, used, path, result) {
  for (let i = 0; i < used.length; i++) {
    if (used[i]) {
      used[i] = false;
      path.push(nums[i]);
      if (path.length === nums.length) {
        result.push([...path]);
      } else {
        permutePath(nums, used, path, result);
      }
      path.pop();
      used[i] = true;
    }
  }
}
/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var permute = function (nums) {
  const used = nums.map(() => true);
  const path = [];
  const result = [];
  permutePath(nums, used, path, result);
  return result;
};
// @lc code=end

// console.log(permute([1, 2, 3]))